<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

JFX::checkAdminPermsRedirect('core', 'perm_manage_users');

$THEME->addBreadcrumb($this->adminUrl.'/users/', $this->lang('ap_title_users'));

$workspaces = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}workspaces");

if(Antz_IntelliForm::submitted('new-be-user')){
    $email = post('email');
    $name = post('name');
    $password = post('password');
    $password2 = post('confPassword');
    $username = post('username');

    if($DB->countRows($CONFIG->dbprefix.'admin_users', "username = '{$username}'")>0){
        $error = true;
        JFX::addError($this->lang('username_already_registered'));
        JFX::addFormError('username');
    }
    if($password != $password2){
        $error = true;
        JFX::addError($this->lang('password_confirmation_no_match'));
        JFX::addFormError('confPassword');
    }
    if(strlen($password)<6){
        $error = true;
        JFX::addError($this->lang('password_too_short'));
        JFX::addFormError('password');
    }
    $selectedWorkspaces = array();

    foreach($workspaces as $k=>$v){
        if(post('ws_'.$v['id'])==1){
            $selectedWorkspaces[] = $v['id'];
        }
    }

    if(count($selectedWorkspaces)==0){
        $error = true;
        JFX::addError($this->lang('no_workspace_selected'));
    }else{
        if(!in_array(post('ws_default'), $selectedWorkspaces)){
            $error = true;
            JFX::addError($this->lang('default_workspace_must_be_accessible'));
        }
    }

    if(!$error){

        $dbParams = array(
            'email' => $email,
            'full_name' => $name,
            'username' => $username,
            'pass' => $CRYPT->crypt($password)
        );
        $userid = $DB->insert($CONFIG->dbprefix.'admin_users', $dbParams);

        foreach($selectedWorkspaces as $k=>$v){
            if(post('ws_default')==$v) $isDefault = 1;
            else $isDefault = 0;

            $dbParams = array(
                'user_id' => $userid,
                'workspace_id' => $v,
                'is_default' => $isDefault
            );
            $DB->insert($CONFIG->dbprefix.'admin_users_to_workspaces', $dbParams);
        }

        JFX::addSuccess($this->lang('success_new_user'));
        JFX::redirect($CONFIG->adminUrl.'/perms/?user='.$userid.'&workspace='.post('ws_default'));

    }

}


$wsOpts = array();
$defWsOpts = array();
foreach($workspaces as $k=>$v){
    $wsOpts[] = array(
        'type' => 'checkbox',
        'name' => 'ws_'.$v['id'],
        'label' => $v['title'],
        'value' => 1,
        'checked' => post('ws_'.$v['id'], 0)
    );
    $defWsOpts[$v['id']] = $v['title'];
}


$newFormFields = array(
    array(
        'name' => 'username',
        'type' => 'text',
        'value' => post('username'),
        'label' => $this->lang('new_username')
    ),
    array(
        'name' => 'name',
        'type' => 'text',
        'value' => post('name'),
        'label' => $this->lang('new_name')
    ),
    array(
        'name' => 'email',
        'type' => 'text',
        'value' => post('email'),
        'class' => 'wide',
        'label' => $this->lang('new_email')
    ),
    array(
        'name' => 'password',
        'type' => 'password',
        'value' => '',
        'label' => $this->lang('new_password')
    ),
    array(
        'name' => 'confPassword',
        'type' => 'password',
        'value' => '',
        'label' => $this->lang('new_confirm_password')
    ),
    array(
        'name' => 'ws_default',
        'type' => 'radiogroup',
        'label' => $this->lang('new_default_workspace'),
        'value' => post('ws_default', '1'),
        'options' => $defWsOpts
    ),
    array(
        'type' => 'longtag',
        'tag' => 'label',
        'content' => $this->lang('new_workspaces')
    )
);

foreach($wsOpts as $k=>$v){
    $newFormFields[] = $v;
}

$newFormFields[] = array(
    'name' => 'submitBtn',
    'type' => 'submit',
    'value' => $this->lang('new_submit'),
    'label' => '&nbsp;'
);


$newForm = JFX::makeRapidForm($newFormFields, $this->lang('new_legend'), 'new-be-user');

$SMARTY->assign('newForm', $newForm->render());

$users = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}admin_users ORDER BY username ASC");

$SMARTY->assign('users', $users);

$content = $SMARTY->fetch($CONFIG->templatesDir.'/admin/users.tpl');

echo $content;